<?php
/**
 * 		批量导出订单服务层
 *      [Haidao] (C)2013-2099 Dmibox Science and technology co., LTD.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      http://www.haidao.la
 *      tel:400-600-2042
 */
class order_exl_service extends service {


	public function __construct() {
		$this->table_sku = $this->load->table('order/order_sku');
		$this->table = $this->load->table('order/order');
		$this->table_trade = $this->load->table('order/order_trade');
		$this->table_supplier = $this->load->table('supplier/supplier');

	}
    
    //按指定条件查找数据
	public function message($sqlmap,$supplier_name){
	  $sku = "`hd_order_sku`";
      $order = config("DB_PREFIX").'order';
      $trade = config("DB_PREFIX").'order_trade';
      $supplier = config("DB_PREFIX").'supplier';

         $orders = $this->table_sku->join($order.' AS A'.' ON '. "$sku.order_sn = A.sn" )->join($trade.' AS C'.' on '."$sku.order_sn = C.order_sn")->join($supplier.' AS D'.' on '."$sku.seller_id = D.id")->where($sqlmap)->field(" C.time, $sku.sub_sn,C.method,C.trade_no,A.address_name,A.address_detail,A.address_mobile,$sku.sku_name,$sku.sku_spec,$sku.sku_price,$sku.real_price,$sku.buy_nums,D.supplier_name,$sku.sku_barcode, $sku.seller_id,$sku.delivery_status,A.delivery_amount")->select();

       if(empty($orders)){
       	echo '<script>alert("您所选条件没有数据");location.href="'.url('index').'"</script>';die;
       }

foreach ($orders as $k => $v) {
	$orders[$k]['money'] = $v['sku_price']*$v['buy_nums'];
	$orders[$k]['sku_spec'] = json_decode($v['sku_spec'],true)[0]['value'];
}

        return $orders;
	}	

  /**
   * 生成查询条件
   * @param  $options['type'] (1:待付款|2:待确认|3:待发货|4:待收货|5:已完成|6:已取消|7:已回收|8:已删除)
   * @param  $options['keyword']  关键词(订单号|收货人姓名|收货人手机)
   * @return [$sqlmap]
   */
  public function build_sqlmap($options) {
    // print_r($options);die;
    $sqlmap = array();
    
      switch ($options) {
         // 已付款
        case 0:
          $sqlmap['C.status']     = 1;
          $sqlmap['A.status'] = 1;
          break;
        // 待付款
        case 1:
          $sqlmap['C.status']    = 0;
          $sqlmap['A.status'] = 1;
          break;
        // 待发货
        case 2:
          $sqlmap['C.status'] = 1;
          $sqlmap['A.confirm_status'] = array('IN',array(1,2));
          $sqlmap['A.delivery_status'] = array('IN',array(0,1));
          break;
        // 待收货
        case 3:
          $sqlmap['C.status'] = 1;
          $sqlmap['A.confirm_status'] = array('IN',array(1,2));
          $sqlmap['hd_order_sku.delivery_status'] = array('IN',array(1,2));
          break;
        // 已完成
        case 4:
          $sqlmap['C.status'] = 1;
          $sqlmap['A.finish_status'] = 2;
          break;
        // 已取消
        case 5:
          $sqlmap['A.status'] = 2;
          break;
        // 前台已删除
        case 6:
          $sqlmap['A.status'] = 4;      
    }
    return $sqlmap;
  }
}

